﻿package {
        import fl.transitions.*;
        import fl.transitions.easing.*;
        import flash.display.DisplayObject;
        import flash.display.Sprite;
		import flash.display.Shape;
        import flash.display.MovieClip;
        import flash.display.Loader;
		
        import flash.display.LoaderInfo;
        import flash.text.*;
        import flash.system.System;
        import flash.events.MouseEvent;
        import flash.display.Stage;
        import flash.events.Event;
        import flash.xml.XMLDocument;
        import flash.net.URLLoader;
        import flash.net.URLRequest;
        
        
        import flash.filters.GlowFilter;
        import flash.utils.Timer;
        import flash.events.TimerEvent;
        import fl.controls.Button;
        import flash.geom.*;

        public class Main extends Sprite {
                //定义属性
                private var firstLoad:Boolean =true;
                private var rect:Sprite;
                private var rectMask:Sprite =new Sprite;
                private var pageContainer:Sprite= new Sprite;
                private var picBgMc:MovieClip = new MovieClip;
				private var picAreaMc:MovieClip = new MovieClip;
                private var currentNum:uint = 0;
                private var len:uint;
                private var XmlUrl:URLRequest;
                private var XmlLoader:URLLoader;
                private var PicUrl:URLRequest;
                private var ThumbUrl:URLRequest;
                private var loader:Loader;
                private var Text:TextField;
                private var BtnPrev:Button;
                private var BtnNext:Button;
                //private var _picGlowFilter:GlowFilter = new GlowFilter(0x999999,1,3,3,3,2,false,false);
                //private var _glowFilter:GlowFilter = new GlowFilter(0x000000,.8,2,2,4,1,true,false);
                var PicUrlAry:Array = new Array();
                var PicTextAry:Array = new Array();

                public function Main() {
					init();//初始化
                }
                private function init():void {
                    //处理XML文件，处理完成后加载第一张大图和缩略图
                    XmlUrl = new URLRequest("info.xml");
                    XmlLoader = new URLLoader();
                    XmlLoader.load(XmlUrl);
                    XmlLoader.addEventListener(Event.COMPLETE,completeHandler);
                    //System.useCodePage = true;
                }
                
                //xml载入完成
                function completeHandler(event:Event){
                    var Xml:XML = new XML(event.target.data);
                    trace(Xml)
					var picNodes :XMLList  = Xml.elements("item");
					len = picNodes.length();
					
                    for(var i:uint=0;i<len;i++){															
                            PicUrlAry.push((picNodes[i].@url8).toString());
                            PicTextAry.push((picNodes[i].@t).toString());
                    }
                    //载入第一张图片和缩略图
					initTextArea();
					viewPage();
                    loadPic(currentNum);
						
                }
                        
                //载入大图
                function loadPic(num:uint):void {
                    PicUrl = new URLRequest(PicUrlAry[num]);
                    loader = new Loader();
                    loader.contentLoaderInfo.addEventListener(Event.COMPLETE,PicLoadcomplete);
                    loader.load(PicUrl);
                    showText(num);
					selectPage(num);
				} 
				
				function viewPage(){
					pageContainer.x = 200;
					pageContainer.y = 520;
					
					var preBtn = new PreBtn();
					preBtn.x = -30;
					preBtn.buttonMode = true;
					preBtn.useHandCursor = true;
					pageContainer.addChild(preBtn);

					var style:StyleSheet = new StyleSheet();
					var a = {color:"#004276", textAlign:"center", fontSize:11, fontStyle:"Tahoma", fontWeight:"bold"}
					style.setStyle("a", a);
							
				 	for(var i = 0; i < len; i++){								
    					var no:TextField = new TextField();
   						no.styleSheet = style;
    					no.htmlText = "<a href='event:void(0);'>"+(i+1)+"</a>";
						no.width = 20;
						no.height = 19;
						no.name = "textPage_" + i;
						no.x = i * 21;
						no.background = true;
						no.backgroundColor = 0xffffff;
						no.addEventListener(MouseEvent.CLICK, clickPageNoHandler);
						no.addEventListener(MouseEvent.MOUSE_OVER, mouseOverPageNoHandler);
						no.addEventListener(MouseEvent.MOUSE_OUT, mouseOutPageNoHandler);
    					pageContainer.addChild(no);							
					}
					
					var nextBtn = new NextBtn();
					nextBtn.x = 190 + 23;
					pageContainer.addChild(nextBtn);
					addChild(pageContainer);
				}
				
				private var historyText;
				function selectPage(num){
					var obj;
					if(typeof num == "number")
						obj = pageContainer.getChildByName("textPage_" + num);
					else 
						obj = num;
					if(historyText){
							historyText.background = 0xffffff;
						}
						obj.backgroundColor = 0x91acc6;
						historyText = obj;
				}				
				
				function clickPageNoHandler(evt:MouseEvent):void{
					var obj = evt.target;
					//selectPage(obj);
						
				}
				
				function mouseOverPageNoHandler(evt:MouseEvent):void{
					evt.target.backgroundColor = 0x9cb7d2;
				}
				
				function mouseOutPageNoHandler(evt:MouseEvent):void{
					var obj = evt.target;
					if(obj.backgroundColor != 0x91acc6)
						obj.backgroundColor = 0xffffff;
				}
				
                //载入大图完成后
                function PicLoadcomplete(evt:Event):void{
					var pic =  evt.target.content;
                    var picWidth = pic.width;
                    var picHeight = pic.height;
					picBgMc.x = (560  - picWidth) /2;
					picBgMc.y = (510  - picHeight) /2;
                    picBgMc.addChildAt((pic), 0);//加载在最底层
					picBgMc.buttonMode = true;
					picBgMc.useHandCursor = true;
        			addChildAt(picBgMc, 0);
                    //RandomInMask(mc);//随机遮罩显示
                    if(!firstLoad){
						picBgMc.removeChildAt(1);//移除上层的图片。
                    }
                    firstLoad = false;						
					picBgMc.addEventListener(MouseEvent.MOUSE_DOWN, nextPicHandler);
                }
				
                //随机遮罩显示大图
                function RandomInMask(_mc:MovieClip){
                    var ran : Number;
                    ran = Math.round( Math.random() * 10 );
                    switch(ran){
                        case 1 :TransitionManager.start(_mc,{type:Blinds, direction:Transition.IN, duration:2, easing:None.easeNone, numStrips:10, dimension:0}); break;
                        case 2 :TransitionManager.start(_mc,{type:Blinds, direction:Transition.IN, duration:2, easing:None.easeNone, numStrips:10, dimension:1}); break;
                        case 3 :TransitionManager.start(_mc, {type:Iris, direction:Transition.IN, duration:2, easing:Strong.easeOut, startPoint:1, shape:Iris.CIRCLE}); break;
                        case 4 :TransitionManager.start(_mc, {type:Iris, direction:Transition.IN, duration:2, easing:Strong.easeOut, startPoint:2, shape:Iris.SQUARE}); break;
                        case 5 :TransitionManager.start(_mc, {type:Iris, direction:Transition.IN, duration:2, easing:Strong.easeOut, startPoint:5, shape:Iris.CIRCLE}); break;
                        case 6 :TransitionManager.start(_mc, {type:Iris, direction:Transition.IN, duration:2, easing:Strong.easeOut, startPoint:8, shape:Iris.SQUARE}); break;
                        case 7 :TransitionManager.start(_mc, {type:Fade, direction:Transition.IN, duration:9, easing:Strong.easeOut});
                        case 8 :TransitionManager.start(_mc, {type:PixelDissolve, direction:Transition.IN, duration:2, easing:Regular.easeIn, xSections:10, ySections:10}); break;
                        case 9 :TransitionManager.start(_mc, {type:Zoom, direction:Transition.IN, duration:2, easing:Elastic.easeOut});
                        default :TransitionManager.start(_mc, {type:Wipe, direction:Transition.IN, duration:2, easing:None.easeNone, startPoint:1}); break;
                    }
                }              
				
				//初始化文字区域
				function initTextArea(){
					picAreaMc.x = 5;
					picAreaMc.y = 5;
					picAreaMc.width = 550;
					picAreaMc.height = 500;
				
					rect = new Sprite();
                    rect.graphics.beginFill(0x000000,.5);
                    rect.graphics.drawRect(10,432,550,74);
                    rect.graphics.endFill();
                    Text = new TextField();
                    Text.text = "";
                    Text.width=550;
                    Text.height=74;
                    Text.textColor=0xFFFFFF;
					
                    Text.x=10;
                    Text.y=432;
                    Text.autoSize="left";
                    rect.addChild(Text);
					addChild(rect);
				}
                
                //显示文字
                function showText(num:uint){
					Text.text = PicTextAry[num];
                }

                //点击图片翻页
                function nextPicHandler(event:MouseEvent):void {
                    if(currentNum<len-1){
                        currentNum++;
                        loadPic(currentNum);
                    }else{ 
						currentNum=0; loadPic(currentNum);
					}
                    //removeChildAt(1);//卸载之前显示的图片                        
                }
                
        }
}